package com.xjj.cms.service.impl;


import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xjj.cms.dao.CompanyDao;
import com.xjj.cms.entity.Company;
import com.xjj.cms.service.CompanyService;
import com.xjj.core.modules.dao.template.DbUtilsTemplate;
import com.xjj.core.modules.orm.Finder;
import com.xjj.core.modules.orm.Page;

/**
 * 不能注入dao层
 * @author Administrator
 *
 */
//默认将类中的所有函数纳入事务管理.
@Transactional
@Service("companyService")//通过接口注入
public class CompanyServiceImpl implements CompanyService{
	
	protected Logger logger = LoggerFactory.getLogger(CompanyServiceImpl.class);
	
	@Autowired
	private CompanyDao companyDao;
	
	@Autowired
	private DbUtilsTemplate dbUtilsTemplate;

	public Company getCompanyById(Long id) {
		return companyDao.get(id);
	}

	/**qRunner.update(conn, "insert into mc_user_field (" + "id,name,type,sort_order,required,visible)"
			+ "values (?,?,?,?,?,?)", new Object[] { userField.getId(), userField.getName(),
			userField.getType(), userField.getSort_order(), userField.getRequired(),
			userField.getVisible() }); */
	public void saveCompany(Company entity) {
		logger.info("dbUtilsTemplate----- "+dbUtilsTemplate);
		//这样关键是查询出现了错误，没有进行报错。
		//我的测试
		/*String[][] params = {{"中国人"}};
		List list = dbUtilsTemplate.query(Company.class, "select * from company where name like ?","中国人");
		logger.info("list size =-== "+list.size());
		*/
		//实现保存操作
		Object[][] params1 = new Object[][] { { "user1", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L }, { "user2", 16L } };
	    int[] result = 
	    	dbUtilsTemplate.batch("insert into Company (name, parentid) values (?, ?)", params1);
	    logger.info("result--- "+result.length);
	   // p("batch插入，返回相应结果数组" + Arrays.toString(result));
	    
		//dbUtilsTemplate.batch("insert company(name) value(?)", "梁和");
		//String[][] params = {{"2008"},{"2008"}};
		//dbUtilsTemplate.batch("insert into Company(name) values(?)", params);
		//Long count = dbUtilsTemplate.stat("select count(*) from Company");
		//logger.info("count---- "+count);
		this.companyDao.save(entity);
	}
	
	public void updateCompany(Company entity) {
		this.companyDao.update(entity);
	}

	public void deleteCompany(Long id) {
		this.companyDao.delete(id);
	}
	
	public List<Company> getAllCompanyList(){
		return this.companyDao.findAll();
	}

	//要想上百万数据库，就必须分两步走，先获取id 值，再根据id来获取分页的数据，再进行封装
	//1、获取数据库总数、获取id,2、使用in 获取所需要的
	//下一步要加条件进行查询和测试
	public Page<Company> getAllCompanyByHql(Map map, int pageNo, int pageSize) {
		String name = (String)map.get("name");
		Finder f = Finder.create("select bean.id from Company bean where 1=1");
		f.append(" order by bean.id asc");
		if (StringUtils.isNotEmpty(name)) {
			f.append(" and bean.name like :name").setParam("name","%" + name + "%" );
		}
		this.logger.info("hql------ "+f.getOrigHql());
		Page page = this.companyDao.findByFinder(f, pageNo, pageSize);
		List list = page.getResult();
		Long date = System.currentTimeMillis();
		String idString = list.toString();
		idString = idString.substring(1, idString.length()-1);
		Long date1 = System.currentTimeMillis();
		logger.info("字符串转换时间 --- "+(date1-date));
		Long date00 = System.currentTimeMillis();
		String hql = "select bean from Company bean where id in ("+idString+")";
		List resultList = this.companyDao.find(hql);
		Long date11 = System.currentTimeMillis();
		logger.info("in 查询date --- "+(date11-date00));
		page.setResult(resultList);
		return page;
	}

}
